import java.util.HashMap;
import java.util.HashSet;

class Solution1 {
    public int monotoneIncreasingDigits(int n) {
        String str = String.valueOf(n);
        char[] s = str.toCharArray();
        int len = str.length();
        int index  = -1;
        for(int i = 0; i+1<len; i++) {
            if(s[i+1] < s[i]) {
                index = i;
                break;
            }
        }

        if(index!=-1) {
            while(index-1>0 && s[index-1] == s[index]) {
                index--;
            }
            s[index]--;

            System.out.println(len);
            for(int i = index+1; i<len; i++) {
                s[i] = '9';
            }
        }
        return Integer.parseInt(new String(s));
    }
}

class Solution2 {
    public int brokenCalc(int startValue, int target) {
        int count = 0;
        while (target > startValue) {
            if(target %2 == 1) {
                target++;
                count++;
            }else {
                target/=2;
                count++;
            }
        }
        return count;
    }
}

class Solution {
    public int longestConsecutive(int[] nums) {
        int n = nums.length;
        HashSet<Integer>set = new HashSet<>();
        for(int x : nums) {
            set.add(x);
        }
        int ret = 0;
        for(int i = 0; i<n; i++) {
            int x = nums[i];
            if(!set.contains(x-1)) {
                int cur = 1;
                while(set.contains(x+1)) {
                    x++;
                    cur++;
                }
                ret = Math.max(ret,cur);
            }
        }
        return ret;
    }
}


public class Test {
    public static void main(String[] args) {
        int ret = new Solution1().monotoneIncreasingDigits(329);
        System.out.println(ret);
    }
}
